﻿using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using Newtonsoft.Json;
using SnakeGame.Models;
using SnakeGame.Views;
using SnakeGame.Helpers;

namespace SnakeGame.ViewModels
{
    public class EditConfigWindowViewModel : NotifyPropertyChanged
    {
        public ICommand SaveCommand { get; set; }
        public ICommand CancelCommand { get; set; }

        public event EventHandler RequestClose;
        private Dictionary<int, Question> _configDict = new Dictionary<int, Question>();
        private ObservableCollection<Question> _questions;
        public ObservableCollection<Question> QuestionList
        {
            get { return _questions; }
            set
            {
                _questions = value;
                OnPropertyChanged("QuestionList");
            }
        }

        public EditConfigWindowViewModel()
        {
            SaveCommand = new RelayCommand(OnSaveCommand);
            CancelCommand = new RelayCommand(OnCancelCommand);

        }

        public void Initialize()
        {
            try
            {
                List<Question> myList = ConfigManager.ReadConfigFromFile();

                 _questions = new ObservableCollection<Question>();
                foreach (var i in myList)
                {
                    _questions.Add(i);
                }
                
                #region Test Write
                //List<Question> myQuestions = new List<Question>();

                //foreach (var i in myList)
                //{
                //    Question a = new Question
                //    {
                //        ID = i.ID,
                //        IsUsed = true,
                //        Quest = i.Quest,
                //        Win = i.Win,
                //        Lose = i.Lose,
                //        Timeout = i.Timeout,
                //        Ladder = i.Ladder,
                //        Retoss = i.Retoss,
                //    };
                //    myQuestions.Add(a);
                //}

                //string json = JsonConvert.SerializeObject(myQuestions, Formatting.Indented);
                //File.WriteAllText(@"c:\config.json", json);
                #endregion

            }
            catch
            {
                MessageBox.Show("No configuration file: config.json");
            }
        }

        private void OnSaveCommand(object obj)
        {
            string exePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            exePath = Path.Combine(exePath, "config.json");
            ConfigManager.WriteConfigToFile(QuestionList.ToList(), exePath);
            OnRequestClose();
        }


        private void OnCancelCommand(object obj)
        {
            OnRequestClose();
        }

        protected void OnRequestClose()
        {
            if (RequestClose != null)
            {
                RequestClose(this, EventArgs.Empty);
            }
        }

    }
}
